$(function () {
    //客户加载
    $("#xsmx_kh").qyjgzcombobox();
    $("#grid_xsmx").datagrid({
        url: $.webPath() + "/xsgl/xsmx/xsmxList",
        toolbar: "#toolbar_xsmx",
        fitColumns: false,
        singleSelect: true,
        fit: true,
        pageNumber: 1,
        striped: true,
        pagination: true,
        rownumbers: true,
        showFooter: true,
        columns: [
            [
                {field: 'xid', hidden: true},
                {field: 'cid', hidden: true},
                {field: 'htbm', title: '销售合同号', align: 'center', width: 110},
                {field: 'xhtbm', title: '来源系统合同号', align: 'center', width: 100},
                {field: 'kh', title: '客户', align: 'center', width: 200},
                {field: 'chtbm', title: '采购合同编码', align: 'center', width: 110},
                {field: 'qdrq', title: '签订日期', align: 'center', width: 90},
                {field: 'fksj', title: '付款时间', align: 'center', width: 90},
                {field: 'sbsl', title: '采购重量', align: 'center', width: 90,
                    formatter: function (value, row) {
                        if (value)
                            return "<a href='javascript:void(0);' style='color: red' onclick='cgslDetail(" + row.cid + ")' >" + value + "</a>"
                        else
                            return value;
                    }},
                {field: 'cgje', title: '采购金额', align: 'center', width: 90},
                {field: 'rksl', title: '入库重量', align: 'center', width: 90,
                    formatter: function (value, row) {
                        return "<a href='javascript:void(0);' style='color: red' onclick='rkslDetail(" + row.xid + ")' >" + value + "</a>"
                    }
                },
                {field: 'cksl', title: '出库重量', align: 'center', width: 90,
                    formatter: function (value, row) {
                        return "<a href='javascript:void(0);' style='color: red' onclick='ckslDetail(" + row.xid + ")' >" + value + "</a>"
                    }
                },
                {field: 'sysl', title: '剩余重量', align: 'center', width: 90},
                {field: 'ghje', title: '购货金额', align: 'center', width: 90},
                {field: 'shje', title: '赎货金额', align: 'center', width: 90,
                    formatter: function (value, row) {
                        return "<a href='javascript:void(0);' style='color: red' onclick='jfmxDetail(" + row.xid + ")' >" + value + "</a>"
                    }
                },
                {field: 'zt', title: '合同状态', align: 'center', width: 90,
                    formatter: function (value) {
                        if (value == 'ysp')
                            return '已审批';
                        if (value == 'yzz')
                            return '已终止';
                        if (value == 'yly')
                            return '已完结';
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                var hsj = Math.round(this.hsj * 10000) / 10000;
                var cksl = Math.round(this.ck_sbsl * 10000) / 10000;
                if (this.hsj) {
                    this.hse = Math.round(multiply(cksl, this.hsj) * 100) / 100;
                }
                if (this.bhsj) {
                    this.bhse = Math.round((this.hse / (1 + this.sl / 100)) * 100) / 100;
                }
            })
            return data;
        }
    });

    //销售明细导出
    $("#btn_xsmx_export").click(function () {
        //var gcmc = $('#xsmx_gc').val();
        var gc = $("#toolbar_xsmx input[name='xsmx_gc']").val();
        var xsdh = $('#xsmx_xsdh').val();
        var sbgsId = $('#xsmxsbgs_jc').combotree('getValue') == null ? null : $('#xsmxsbgs_jc').combotree('getValue');//公司的Id获取
        var sbbmId = $('#xsmxsbbm_jc').combotree('getValue') == null ? null : $('#xsmxsbbm_jc').combotree('getValue');//部门的Id获取
        var khId = $("#xsmx_kh").val();
        var ggName = $("#xsmx_name").val();
        var ggCode = $("#xsmx_code").val();
        var ggXh = $("#xsmx_ggxh").val();
        var fhrqStart = $('#xsmx_fhrq_start').datebox('getValue');
        var fhrqEnd = $('#xsmx_fhrq_end').datebox('getValue');
        var jszt = $("#xsmx_xsd_jszt").combobox('getValue');
        var ckId = $("#toolbar_xsmx input[name='xsmx_ck']").val();
        var xslx = $("#xsmx_xsd_xslx").combobox('getValue');
        var djlx = $("#xsmx_xsd_djlx").combobox('getValue');
        var zt = $("#xsmx_xsd_wczt").combobox('getValue');
        window.open($.webPath() + "/xsgl/xsmx/xsmx_export?xsdh=" + xsdh + "&&sbgsId=" + sbgsId + "&&sbbmId=" + sbbmId + "&&khId=" + khId
            + "&&ggName=" + ggName + "&&ggCode=" + ggCode + "&&ggXh=" + ggXh + "&&fhrqStart=" + fhrqStart + "&&fhrqEnd=" + fhrqEnd + "&&zt=" + zt + "&&gcId=" + gc + "&&jszt=" + jszt + "&&ckId=" + ckId + "&&xslx=" + xslx + "&&djlx=" + djlx);
    });

})

// 采购明细
function cgslDetail(cid) {
    $.window({
        title: '采购合同明细',
        iconCls: 'icon-tip',
        url: $.webPath() + "/xsgl/xsmx/cghtgldetail?cghtId=" + cid,
        width: 1050,
        height: 500,
        modal: true,
        isMax: false
    })

}

// 入库明细
function rkslDetail(xid) {
    $.window({
        title: '库存明细',
        iconCls: 'icon-tip',
        url: $.webPath() + "/xsgl/xsmx/sskcdetail?kclx=0&xshtId=" + xid,
        width: 1050,
        height: 500,
        modal: true,
        isMax: false
    })
}

// 出库明细
function ckslDetail(xid) {
    $.window({
            title: '库存明细',
            iconCls: 'icon-tip',
            url: $.webPath() + "/xsgl/xsmx/sskcdetail?kclx=1&xshtId=" + xid,
            width: 1050,
            height: 500,
            modal: true,
            isMax: false
        }
    )
}

// 计费明细
function jfmxDetail(xid) {
    $.window({
            title: '销售明细',
            iconCls: 'icon-tip',
            url: $.webPath() + "/xsgl/xsmx/xsmxdetail?xshtId=" + xid,
            width: 1350,
            height: 500,
            modal: true,
            isMax: false
        }
    )
}


// 查询
$("#btn_xsmx_search").click(function () {
    xsmxSearch();
});


function xsmxSearch() {
    $('#grid_xsmx').datagrid('load',
        {
            htbm: $('#htbm').val(),
            xhtbm: $('#xhtbm').val(),
            khId: $("#xsmx_kh").val(),
            fhrqStart: $('#xsmx_fhrq_start').datebox('getValue'),
            fhrqEnd: $('#xsmx_fhrq_end').datebox('getValue'),
            zt: $("#xsmx_xsd_wczt").combobox('getValue')
        }
    );
}